//
//  YLCQuestionView.m
//  LinYa
//
//  Created by 初程程 on 2018/2/5.
//  Copyright © 2018年 初程程. All rights reserved.
//

#import "YLCQuestionView.h"
#import "YLCQuestionCell.h"
#import "YLCDynamicInfoViewController.h"
#import "YLCQuestionModel.h"
#define QUESTION_CELL @"YLCQuestionCell"
@interface YLCQuestionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation YLCQuestionView
{
    UICollectionView *questionTable;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.reloadSignal = [RACSubject subject];
        [self createCustomView];
    }
    return self;
}
- (void)createCustomView{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    questionTable = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
    
    questionTable.alwaysBounceVertical = YES;
    
    questionTable.backgroundColor = YLC_COMMON_BACKCOLOR;
    
    [questionTable registerClass:[YLCQuestionCell class] forCellWithReuseIdentifier:QUESTION_CELL];
    
    questionTable.delegate = self;
    
    questionTable.dataSource = self;
    
    [questionTable setupNormalRefreshHeadAndFootWithTarget:self headAction:@selector(reloadData) footAction:@selector(didTriggerLoadNextPageData)];
    
    [self addSubview:questionTable];
    
    
}
- (void)setQuestionList:(NSArray *)questionList{
    
    _questionList = questionList;
    
    [questionTable reloadData];
    
    [questionTable.mj_header endRefreshing];
}
- (void)beganRefresh{
    [questionTable.mj_header beginRefreshing];
}
- (void)resetNoMore{
    [questionTable.mj_footer endRefreshing];
}
- (void)loadedAllData{
    [questionTable.mj_footer endRefreshingWithNoMoreData];
}
- (void)reloadData{
    [self.reloadSignal sendNext:@"0"];
}
-(void)setIsMine:(BOOL)isMine{
    _isMine = isMine;
}
- (void)setIsNeedDel:(BOOL)isNeedDel{
    _isNeedDel = isNeedDel;
}
- (void)didTriggerLoadNextPageData{
    
    NSInteger page = [_questionList count]/[PAGE_LIMIT integerValue];
    
    [self.reloadSignal sendNext:[NSString stringWithFormat:@"%ld",page]];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _questionList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YLCQuestionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:QUESTION_CELL forIndexPath:indexPath];
    
    if (indexPath.row<_questionList.count) {
        
        YLCQuestionModel *model = _questionList[indexPath.row];
        
        cell.model = model;
        
        [cell configUI];
        
        if (_type == 0) {
            if (_isNeedDel) {
                cell.isNeedDel = YES;
                cell.deleteBtn.hidden = NO;
                cell.deleteBtn.tag = indexPath.row;
                [cell.deleteBtn addTarget:self action:@selector(deleteInfo:) forControlEvents:UIControlEventTouchUpInside];
            }else{
                cell.deleteBtn.hidden = YES;
            }
        }else{
            cell.deleteBtn.hidden = YES;
        }
    }
    return cell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    YLCQuestionModel *model = _questionList[indexPath.row];
    CGFloat heigth = 120;
    CGSize questionSize = [YLCFactory getStringSizeWithString:model.desc width:self.frameWidth-24 font:15];
    CGSize replySize = CGSizeZero;
    if (!JK_IS_DICT_NIL(model.solve)) {
        replySize = [YLCFactory getStringSizeWithString:model.solve[@"content"] width:self.frameWidth-24-28 font:15];
        
        replySize.height += 34;
    }
    
    return CGSizeMake(self.frameWidth, heigth+questionSize.height+replySize.height);
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YLCDynamicInfoViewController *con = [[YLCDynamicInfoViewController alloc] init];
    
    YLCQuestionModel *model = _questionList[indexPath.row];
    
    con.caseId = model.id;
    
    con.type = 0;
    
    [(UINavigationController *)[YLCFactory currentNav] pushViewController:con animated:YES];
    
}
#pragma mark -- 问答删除
- (void)deleteInfo:(UIButton*)button{
    YLCQuestionModel *model = _questionList[button.tag];
    YLCAccountModel *userModel = [[YLCAccountManager shareManager] currentAccount];
    [YLCHUDShow showSelectAlertWithTarget:self Message:@"是否确认删除" leftTitle:@"确认" rightTitle:@"取消" selector:^(NSInteger index) {
        if (index==0) {
            NSDictionary *parms = @{@"userId":userModel.user_id,
                                    @"token":userModel.token,
                                    @"caseId":model.id};
            [YLCNetWorking loadNetServiceWithParam:parms method:@"POST" urlPath:@"app/case/del" delegate:self response:^(id responseObject, NSError *error) {
                if (error) {
                    return ;
                }
                [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                
                NSMutableArray *container = [_questionList mutableCopy];
                
                [container removeObject:model];
                
                _questionList = [container copy];
                
                [questionTable reloadData];
            }];
        }
    }];
}
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    [questionTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
